package cn.gs.util.stream;

// @author gs
import java.io.*;

public class StringStreamUtil {

    public static InputStream getInputStream(String s) {
        return new ByteArrayInputStream(s.getBytes());
    }

    public static InputStream getInputStream(String s, String charset) throws UnsupportedEncodingException {
        return new ByteArrayInputStream(s.getBytes(charset));
    }

    public static Reader getReader(String s) {
        return new StringReader(s);
    }

    public static String readLine(InputStream is, String charset) throws IOException {
        BufferedReader sr = new BufferedReader(new InputStreamReader(is, charset));
        return sr.readLine();
    }

    public static String readLine(InputStream is) throws IOException {
        BufferedReader sr = new BufferedReader(new InputStreamReader(is));
        return sr.readLine();
    }

    public static String getString(InputStream is, String charset) throws IOException {
        return getString(new InputStreamReader(is, charset));
    }

    public static String getString(InputStream is) throws IOException {
        return getString(new InputStreamReader(is));
    }

    public static String getString(Reader r) throws IOException {
        StringBuilder sb = new StringBuilder();
        char[] cs = new char[1024];
        int len;
        while ((len = r.read(cs)) > 0) {
            sb.append(new String(cs, 0, len));
        }
        return sb.toString();
    }
}
